﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using TakePhotoTogether.Dao;
using TakePhotoTogether.Model;

namespace TakePhotoTogether
{
    public class PlaceHolderManager
    {
        private const string photosDirectoryName = "Photos";
        private const string placeholder_to_be_removed_storagekey = "received_photos";

        public static void DeleteAllPlaceHolders()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Debug.WriteLine("PlaceHolderManager::DeletePlaceHolder - ");

                if (store.DirectoryExists(photosDirectoryName))
                {
                    String[] fileNames = store.GetFileNames(photosDirectoryName + "\\*");
                
                    // Delete all the files currently in the Archive directory. 
                    if (fileNames.Length > 0)
                    {
                        for (int i = 0; i < fileNames.Length; ++i)
                        {
                            // Delete the files.
                            store.DeleteFile(photosDirectoryName + "\\" + fileNames[i]);
                        }
                    }
                }


            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="photoFileName"></param>
        public static void DeletePlaceHolder(string photoFileName)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Debug.WriteLine("PlaceHolderManager::DeletePlaceHolder - " + photoFileName);

                if (!store.GetDirectoryNames().Contains<string>(photosDirectoryName))
                {
                    store.CreateDirectory(photosDirectoryName);
                }

                string[] photoFileNames = { "Photos\\" + photoFileName, "Photos\\" + photoFileName + ".thumb.jpg" };

                foreach (string fileName in photoFileNames)
                {
                    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        storage.DeleteFile(fileName);
                    }
                }
            }
        }

        public static void AddPlaceHolder(string photoName)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.GetDirectoryNames().Contains<string>(photosDirectoryName))
                {
                    store.CreateDirectory(photosDirectoryName);
                }

                string[] photoFileNames = { "Photos\\" + photoName, "Photos\\" + photoName + ".thumb.jpg" };

                foreach (string photoFileName in photoFileNames)
                {
                    using (Stream input = Application.GetResourceStream(new Uri("Photos\\PlaceHolder.png", UriKind.Relative)).Stream)
                    {
                        using (IsolatedStorageFileStream output = store.CreateFile(photoFileName))
                        {
                            byte[] readBuffer = new byte[4096];
                            int bytesRead = -1;

                            // Copy the file from the installation folder to isolated storage. 
                            while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                output.Write(readBuffer, 0, bytesRead);
                            }
                        }
                    }
                }
            }
        }


        public static void MarkPlaceHolderToBeRemoved(string name)
        {
            StorageQueue queue = IsolatedStorageDao.Load<StorageQueue>(placeholder_to_be_removed_storagekey);

            if (queue == null || queue.pictures == null)
            {
                queue = new StorageQueue();
                queue.pictures = new string[1];
            }
            else
            {
                string[] pictures = queue.pictures;
                Array.Resize(ref pictures, queue.pictures.Length + 1);
                queue.pictures = pictures;
            }

            queue.pictures[queue.pictures.Length - 1] = name;

            Debug.WriteLine("PlaceHolderManager::MarkPlaceHolderToBeRemoved - size of queue - " + queue.pictures.Length);

            IsolatedStorageDao.Save(queue, placeholder_to_be_removed_storagekey);
        }

        public static void DeleteAllMarkedPlaceholders()
        {
            StorageQueue queue = IsolatedStorageDao.Load<StorageQueue>(placeholder_to_be_removed_storagekey);
            IsolatedStorageDao.Save(null, placeholder_to_be_removed_storagekey);

            if (queue != null && queue.pictures != null)
            {
                foreach (string s in queue.pictures)
                {
                    DeletePlaceHolder(s);
                }
            }
        }
    }
}
